import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_widget/base/base_app.dart';

//静态路由配置
void main() => runApp(BaseApp(homeWidget: Exam205HomePage()));

class Exam205HomePage extends StatefulWidget {
  @override
  State<Exam205HomePage> createState() => _Exam205HomePageState();
}

///创建多评阅流控制器
StreamController<String> streamController = StreamController.broadcast();
StreamController<String> streamController2 = StreamController();

///lib/code7/code701_main_page.dart
class _Exam205HomePageState extends State<Exam205HomePage> {
  int _data = 0;
  @override
  Widget build(BuildContext context) {
    //Scaffold 用来搭建页面的主体结构
    return Scaffold(
      //页面的主内容区
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                _data++;
                ///第三步就是数据变化后
                ///赋值更新
                streamController.add("$_data");
              },
              child: const Text("+"),
            ),
            const SizedBox(width: 40),
            Column(
              mainAxisSize: MainAxisSize.min,
              children: const [
                BPage(),
                SizedBox(height: 20),
                CPage(),
                SizedBox(height: 20),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class CPage extends StatelessWidget {
  const CPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<String>(
      //绑定stream
      stream: streamController.stream,
      //默认的数据
      initialData: "00:00:00",
      ///构建绑定数据的UI
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        ///snapshot.data 就是传递的数据对象
        return Text(
          'C中更新数据  ${snapshot.data} ',
          style: const TextStyle(fontSize: 22, color: Colors.blue),
        );
      },
    );
  }
}


///代码清单 7-3
class BPage extends StatelessWidget {
  const BPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<String>(
      ///绑定stream
      stream: streamController.stream,
      ///默认的数据 初始
      initialData: "00:00:00",
      ///构建绑定数据的UI
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        ///snapshot.data 就是传递的数据对象
        return Text(
          'B中更新数据  ${snapshot.data} ',
          style: const TextStyle(fontSize: 22, color: Colors.blue),
        );
      },
    );
  }
}

